#!/bin/bash

#set -x
CHANNEL_NAME=mychannel
export FABRIC_CFG_PATH=${PWD}

# Generates Org certs using cryptogen tool
function generateCerts (){
  which cryptogen
  if [ "$?" -ne 0 ]; then
    echo "cryptogen tool not found. exiting"
    exit 1
  fi
  echo
  echo "##########################################################"
  echo "##### Generate certificates using cryptogen tool #########"
  echo "##########################################################"
  if [ -d "/var/fabric-net/crypto-config" ]; then
    rm -Rf /var/fabric-net/crypto-config
  fi
  cryptogen generate --config=./crypto-config.yaml --output=/var/fabric-net/crypto-config
  if [ "$?" -ne 0 ]; then
    echo "Failed to generate certificates..."
    exit 1
  fi
  echo
}

# Using docker-compose-e2e-template.yaml, replace constants with private key file names
# generated by the cryptogen tool and output a docker-compose.yaml specific to this
# configuration
function replacePrivateKey () {
  # Copy the template to the file that will be modified to add the private key
  cp manifests/ca-template/ca0-org1-template.yaml manifests/ca0-org1.yaml
  cp manifests/ca-template/ca0-org2-template.yaml manifests/ca0-org2.yaml

  # The next steps will replace the template's contents with the
  # actual values of the private key file names for the two CAs.
  CURRENT_DIR=$PWD
  cd /var/fabric-net/crypto-config/peerOrganizations/org1.fabric-net.svc.cluster.local/ca/
  PRIV_KEY=$(ls *_sk)
  cd "$CURRENT_DIR"
  sed -i $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" manifests/ca0-org1.yaml
  cd /var/fabric-net/crypto-config/peerOrganizations/org2.fabric-net.svc.cluster.local/ca/
  PRIV_KEY=$(ls *_sk)
  cd "$CURRENT_DIR"
  sed -i $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" manifests/ca0-org2.yaml
}

# Generate orderer genesis block, channel configuration transaction and
# anchor peer update transactions
function generateChannelArtifacts() {
  which configtxgen
  if [ "$?" -ne 0 ]; then
    echo "configtxgen tool not found. exiting"
    exit 1
  fi

  if [ -d "/var/fabric-net/channel-artifacts" ]; then
    rm -Rf /var/fabric-net/channel-artifacts
  fi
  mkdir -p /var/fabric-net/channel-artifacts
  echo "##########################################################"
  echo "#########  Generating Orderer Genesis block ##############"
  echo "##########################################################"
  # Note: For some unknown reason (at least for now) the block file can't be
  # named orderer.genesis.block or the orderer will fail to launch!
  configtxgen -profile TwoOrgsOrdererGenesis -outputBlock \
  /var/fabric-net/channel-artifacts/genesis.block
  if [ "$?" -ne 0 ]; then
    echo "Failed to generate orderer genesis block..."
    exit 1
  fi
  echo
  echo "#################################################################"
  echo "### Generating channel configuration transaction 'channel.tx' ###"
  echo "#################################################################"
  configtxgen -profile TwoOrgsChannel -outputCreateChannelTx \
  /var/fabric-net/channel-artifacts/channel.tx -channelID $CHANNEL_NAME
  if [ "$?" -ne 0 ]; then
    echo "Failed to generate channel configuration transaction..."
    exit 1
  fi

  echo
  echo "#################################################################"
  echo "#######    Generating anchor peer update for Org1MSP   ##########"
  echo "#################################################################"
  configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate \
  /var/fabric-net/channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP
  if [ "$?" -ne 0 ]; then
    echo "Failed to generate anchor peer update for Org1MSP..."
    exit 1
  fi

  echo
  echo "#################################################################"
  echo "#######    Generating anchor peer update for Org2MSP   ##########"
  echo "#################################################################"
  configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate \
  /var/fabric-net/channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
  if [ "$?" -ne 0 ]; then
    echo "Failed to generate anchor peer update for Org2MSP..."
    exit 1
  fi
  echo
}

function prepareEnv() {
	rm -rf /var/fabric-net/chaincode/
	rm -rf /var/fabric-net/production
	rm -rf /tmp/hyperlook

	cp -r chaincode /var/fabric-net/
	cp -r scripts /var/fabric-net/
}


generateCerts
replacePrivateKey
generateChannelArtifacts
prepareEnv

